package com.example.bzstock;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.domob.android.ads.DomobAdEventListener;
import cn.domob.android.ads.DomobAdManager.ErrorCode;
import cn.domob.android.ads.DomobAdView;
import cn.domob.android.ads.DomobUpdater;

import com.example.bzstock.comm.Comm;

public class M1K12Activity extends BaseActivity {

	LinearLayout vContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_m1k12);
		vContent = (LinearLayout) findViewById(R.id.m1_content);
		ini();
		addAD();
	}

	private void ini() {
		addPics();
	}

	void addPics() {
		addTitle("");
		addTitle("");
		addTitle("1_T字线,锤子,吊人,流星,倒T");
		addPic("1");

		addTitle("1_T字线,锤子_2");
		addPic("1_2");

		addTitle("2_白吞噬,黑吞噬");
		addPic("2");

		addTitle("3_母子线");
		addPic("3");

		addTitle("3_母子线_2");
		addPic("3_2");

		addTitle("4_夜星与晨星");
		addPic("4");

		addTitle("5_白执带,黑执带");
		addPic("5");

		addTitle("6_十字弃婴与岛型反转");
		addPic("6");

		addTitle("6_十字弃婴与岛型反转_2");
		addPic("6_2");

		addTitle("7_上升三法下降三法");
		addPic("7");

		addTitle("8_三鸭耀空,红兵排列");
		addPic("8");

		addTitle("8_三鸭耀空,红兵排列_2");
		addPic("8_2");

		addTitle("9_隔离线");
		addPic("9");

		addTitle("10_高跳与低跳");
		addPic("10");

		addTitle("11_k线的弧线");
		addPic("11");

		addTitle("12_多空版图之争");
		addPic("12");

	}

	LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.MATCH_PARENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);

	void addTitle(String title) {
		TextView v = new TextView(this);
		v.setText(title);
		v.setTextColor(this.getResources().getColor(R.color.black));
		v.setPadding(5, 10, 5, 2);
		vContent.addView(v, lp);
	}

	void addPic(String fileName) {
		// 添加图片
		try {
			Bitmap bm = getAssetsBitmpScaledByX_K12(fileName);
			if (null != bm) {
				ImageView v = new ImageView(this);
				v.setImageBitmap(bm);

				// lp_Top.gravity = Gravity.BOTTOM;// 设置广告出现的位置(悬浮于底部)
				vContent.addView(v, lp);
			}
		} catch (Exception e) {
			Comm.print(e.toString());
		}
	}

	protected Bitmap getAssetsBitmpScaledByX_K12(String avName) {
		String path = "k12/" + avName + ".jpg";
		Bitmap bm = getAssetsBitmp(path);
		if (null != bm) {
			int w = bm.getWidth();
			int h = bm.getHeight();
			int th = (int) (h * Comm.screenWidth / w);
			bm = Bitmap.createScaledBitmap(bm, Comm.screenWidth, th, false);
		}
		return bm;
	}

	public Bitmap getAssetsBitmp(String avName) {
		AssetManager assetManager = getAssets();
		Bitmap bm = null;
		try {
			InputStream is = assetManager.open(avName);
			bm = BitmapFactory.decodeStream(is);
		} catch (IOException e) {
			System.out.print(e.getMessage());
		}
		return bm;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	// -------------------添加广告---------------------------------------

	// RelativeLayout mAdContainer; // 广告

	void addAD() {
		DomobAdView mAdview320x50;// 广告
		String PUBLISHER_ID = "56OJyHIYuNSq7GLRMh";
		// String PUBLISHER_ID = "56OJyM1ouMGoaSnvCK";
		//

		String InlinePPID = "16TLwWAaApl-zNUH9L5TbChi";

		RelativeLayout mAdContainer = new RelativeLayout(this);
		// (RelativeLayout) findViewById(R.id.adcontainer);
		// Create ad view
		mAdview320x50 = new DomobAdView(this, PUBLISHER_ID, InlinePPID,
				DomobAdView.INLINE_SIZE_320X50);
		mAdview320x50.setKeyword("game");
		mAdview320x50.setUserGender("male");
		mAdview320x50.setUserBirthdayStr("2000-08-08");
		mAdview320x50.setUserPostcode("123456");

		mAdview320x50.setAdEventListener(new DomobAdEventListener() {

			@Override
			public void onDomobAdReturned(DomobAdView adView) {
				System.out.print("onDomobAdReturned");
			}

			@Override
			public void onDomobAdOverlayPresented(DomobAdView adView) {
				System.out.print("overlayPresented");
			}

			@Override
			public void onDomobAdOverlayDismissed(DomobAdView adView) {
				System.out.print("Overrided be dismissed");
			}

			@Override
			public void onDomobAdClicked(DomobAdView arg0) {
				System.out.print("onDomobAdClicked");
			}

			@Override
			public void onDomobAdFailed(DomobAdView arg0, ErrorCode arg1) {
				System.out.print("onDomobAdFailed");
			}

			@Override
			public void onDomobLeaveApplication(DomobAdView arg0) {
				System.out.print("onDomobLeaveApplication");
			}

			@Override
			public Context onDomobAdRequiresCurrentContext() {
				return M1K12Activity.this;
			}
		});

		mAdContainer.addView(mAdview320x50);

		FrameLayout.LayoutParams lp_Top = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.MATCH_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);
		lp_Top.gravity = Gravity.BOTTOM;// 设置广告出现的位置(悬浮于底部)
		addContentView(mAdContainer, lp_Top);

		// 检查应用更新
		DomobUpdater.checkUpdate(this, "56OJyHIYuMiMpHKWmR");
	}

}
